package com.ruoyi.share.service.impl;

import com.ruoyi.data.DataUtils;
import com.ruoyi.share.domain.HouseRateT;
import com.ruoyi.share.mapper.HouseRateTMapper;
import com.ruoyi.share.service.IHouseRateTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 利息分摊Service业务层处理
 * 
 * @author lijie
 * @date 2023-08-04
 */
@Service
public class HouseRateTServiceImpl implements IHouseRateTService
{
    @Autowired
    private HouseRateTMapper houseRateTMapper;

    /**
     * 查询利息分摊
     * 
     * @param rateId 利息分摊主键
     * @return 利息分摊
     */
    @Override
    public HouseRateT selectHouseRateTByRateId(Long rateId)
    {
        return houseRateTMapper.selectHouseRateTByRateId(rateId);
    }

    /**
     * 查询利息分摊列表
     * 
     * @param houseRateT 利息分摊
     * @return 利息分摊
     */
    @Override
    public List<HouseRateT> selectHouseRateTList(HouseRateT houseRateT)
    {
        return houseRateTMapper.selectHouseRateTList(houseRateT);
    }

    @Override
    public List<HouseRateT> selectHouseRateDetailTByRateId(HouseRateT houseRateT)
    {
        return houseRateTMapper.selectHouseRateDetailTByRateId(houseRateT);
    }

    /**
     * 新增利息分摊
     * 
     * @param houseRateT 利息分摊
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHouseRateT(HouseRateT houseRateT)
    {
        HouseRateT houseRateVO = new HouseRateT();
        if(houseRateT.getRateDate().before(houseRateT.getCostDate())){
            return 2;
        }
        houseRateVO = houseRateTMapper.getBalanceSumForShare(houseRateT);
        List<HouseRateT> houseRateTList = new ArrayList<HouseRateT>();
        houseRateTList = houseRateTMapper.getDoorListForShare(houseRateT);
        Double rateMoney = 0d;
        houseRateVO.setRateType("5");
        houseRateVO.setRateDate(houseRateT.getRateDate());
        houseRateVO.setShouldMoney(houseRateT.getRateMoney());
        houseRateVO.setRemark(houseRateT.getRemark());
        houseRateVO.setCostDate(houseRateT.getCostDate());
        houseRateTMapper.insertHouseRateT(houseRateVO);
        int rateId = houseRateVO.getRateId();
        for(HouseRateT infoVO : houseRateTList){
            infoVO.setTotalInfo(houseRateVO.getTotalInfo());
            if(houseRateVO.getTotalInfo()!=0){
                infoVO.setRateMoney(DataUtils.doubleScale2(infoVO.getDoorInfo()*houseRateT.getRateMoney()/houseRateVO.getTotalInfo()));
                infoVO.setRateType("5");
                infoVO.setRateDate(houseRateT.getRateDate());
                infoVO.setRateId(rateId);
                houseRateTMapper.insertMoneyDetail(infoVO);
                houseRateTMapper.updateDoorRateType(infoVO);
                rateMoney = DataUtils.sumMoney(rateMoney, DataUtils.doubleScale2(infoVO.getDoorInfo()*houseRateT.getRateMoney()/houseRateVO.getTotalInfo()));
            }

        }
        houseRateVO.setRateMoney(rateMoney);
        houseRateVO.setRateId(rateId);
        return houseRateTMapper.updateHouseRateT(houseRateVO);
    }

    /**
     * 修改利息分摊
     * 
     * @param houseRateT 利息分摊
     * @return 结果
     */
    @Override
    public int updateHouseRateT(HouseRateT houseRateT)
    {
        return houseRateTMapper.updateHouseRateT(houseRateT);
    }

    /**
     * 批量删除利息分摊
     * 
     * @param rateIds 需要删除的利息分摊主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHouseRateTByRateIds(Integer[] rateIds)
    {
        for(Integer rateId:rateIds){
            houseRateTMapper.deleteHouseRateTByRateId(rateId);
            houseRateTMapper.deleteHouseRateDetailTByRateId(rateId);
        }
        return houseRateTMapper.updateHouseRateTByDetail();
    }

}
